var t = 0, a = 0, e = wx.getSystemInfoSync().pixelRatio, c = 0, o = 0, i = 0, r = 0, n = 0, s = 0, u = 1;

Page({
    data: {
        imageSrc: "https://lpd.hi-finance.com.cn/20170918001.jpg",
        returnImage: "",
        isShowImg: !1,
        cropperInitW: 750,
        cropperInitH: 750,
        cropperW: 750,
        cropperH: 750,
        cropperL: 0,
        cropperT: 0,
        scaleP: 0,
        imageW: 0,
        imageH: 0,
        cutW: 0,
        cutH: 0,
        cutL: 0,
        cutT: 0,
        qualityWidth: 1080,
        innerAspectRadio: u
    },
    onLoad: function(t) {},
    onReady: function() {},
    getImage: function() {
        var t = this;
        wx.chooseImage({
            success: function(a) {
                t.setData({
                    imageSrc: a.tempFilePaths[0]
                }), t.loadImage();
            }
        });
    },
    loadImage: function() {
        var t = this;
        wx.showLoading({
            title: "图片加载中..."
        }), wx.getImageInfo({
            src: t.data.imageSrc,
            success: function(a) {
                (u = a.width / a.height) >= 1 ? t.setData({
                    cropperW: 750,
                    cropperH: 750 / u,
                    cropperL: Math.ceil(0),
                    cropperT: Math.ceil((750 - 750 / u) / 2),
                    cutW: 550,
                    cutH: 750 / u - 200,
                    cutL: Math.ceil(100),
                    cutT: Math.ceil((750 / u - (750 / u - 200)) / 2),
                    scaleP: a.width * e / 750,
                    imageW: a.width * e,
                    imageH: a.height * e,
                    innerAspectRadio: u
                }) : t.setData({
                    cropperW: 750 * u,
                    cropperH: 750,
                    cropperL: Math.ceil((750 - 750 * u) / 2),
                    cropperT: Math.ceil(0),
                    cutW: 750 * u - 50,
                    cutH: 200,
                    cutL: Math.ceil((750 * u - (750 * u - 50)) / 2),
                    cutT: Math.ceil(275),
                    scaleP: a.width * e / 750,
                    imageW: a.width * e,
                    imageH: a.height * e,
                    innerAspectRadio: u
                }), t.setData({
                    isShowImg: !0
                }), wx.hideLoading();
            }
        });
    },
    contentStartMove: function(e) {
        t = e.touches[0].pageX, a = e.touches[0].pageY;
    },
    contentMoveing: function(e) {
        var c = this, o = 2 * (t - e.touches[0].pageX), i = 2 * (a - e.touches[0].pageY), r = Math.max(c.data.cutL - o, 0), n = Math.max(c.data.cutT - i, 0), s = c.data.cropperW - c.data.cutW, u = c.data.cropperH - c.data.cutH;
        this.setData({
            cutL: Math.min(s, r),
            cutT: Math.min(u, n)
        }), console.log(s + " ----- " + r), t = e.touches[0].pageX, a = e.touches[0].pageY;
    },
    getImageInfo: function() {
        var t = this;
        wx.showLoading({
            title: "图片生成中..."
        });
        var a = wx.createCanvasContext("myCanvas");
        a.drawImage(t.data.imageSrc, 0, 0, 1080, 1080 / u), a.draw(!0, function() {
            var a = t.data.cutW / t.data.cropperW * 1080, c = t.data.cutH / t.data.cropperH * 1080 / u, o = t.data.cutL / t.data.cropperW * 1080, i = t.data.cutT / t.data.cropperH * 1080 / u;
            console.log("canvasW:" + a + " --- canvasH: " + c + " --- canvasL: " + o + " --- canvasT: " + i + " -------- _this.data.imageW: " + t.data.imageW + "  ------- _this.data.imageH: " + t.data.imageH + " ---- pixelRatio " + e), 
            wx.canvasToTempFilePath({
                x: o,
                y: i,
                width: a,
                height: c,
                destWidth: a,
                destHeight: c,
                quality: .5,
                canvasId: "myCanvas",
                success: function(t) {
                    wx.hideLoading(), console.log(t.tempFilePath), wx.previewImage({
                        current: "",
                        urls: [ t.tempFilePath ]
                    });
                }
            });
        });
    },
    dragStart: function(t) {
        var a = this;
        c = t.touches[0].pageX, o = t.touches[0].pageY, i = a.data.cutW, r = a.data.cutL, 
        s = a.data.cutT, n = a.data.cutH;
    },
    dragMove: function(t) {
        var a = this;
        switch (t.target.dataset.drag) {
          case "right":
            e = 2 * (c - t.touches[0].pageX);
            if (!(i >= e)) return;
            if (e < 0 && a.data.cropperW > r + a.data.cutW && this.setData({
                cutW: i - e
            }), !(e > 0)) return;
            this.setData({
                cutW: i - e
            });
            break;

          case "left":
            e = 2 * (e = c - t.touches[0].pageX);
            if (console.log(e), !(i >= e && r > e)) return;
            if (e < 0 && Math.abs(e) >= i) return;
            this.setData({
                cutL: r - e,
                cutW: i + e
            });
            break;

          case "top":
            e = 2 * (o - t.touches[0].pageY);
            if (!(n >= e && s > e)) return;
            if (e < 0 && Math.abs(e) >= n) return;
            this.setData({
                cutT: s - e,
                cutH: n + e
            });
            break;

          case "bottom":
            var e = 2 * (o - t.touches[0].pageY);
            if (console.log(e), console.log(n >= e), console.log(a.data.cropperH > s + a.data.cutH), 
            !(n >= e)) return;
            if (e < 0 && a.data.cropperH > s + a.data.cutH && this.setData({
                cutH: n - e
            }), !(e > 0)) return;
            this.setData({
                cutH: n - e
            });
            break;

          case "rightBottom":
            var u = 2 * (c - t.touches[0].pageX), h = 2 * (o - t.touches[0].pageY);
            if (!(n >= h && i >= u)) return;
            if ((h < 0 && a.data.cropperH > s + a.data.cutH || h > 0) && this.setData({
                cutH: n - h
            }), !(u < 0 && a.data.cropperW > r + a.data.cutW || u > 0)) return;
            this.setData({
                cutW: i - u
            });
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});